package top.kaoshanji.p1ch05b;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * author kaoshanji
 * ^_^
 * create time 2025/2/11 16:17
 */
public class PuttingIntoPractice {

    private static final Logger logger = LogManager.getLogger(PuttingIntoPractice.class);

    public static void main(String[] args) {

        // IDEA 优化提示
        List<Transaction> transactions = getTransactions();

        // 代码清单-p1ch05b-104页a：找出2011年发生的所有交易，并按交易额排序（从低到高）
        logger.info("...找出2011年发生的所有交易，并按交易额排序（从低到高）...");
        List<Transaction> tr2011 = transactions.stream()
                .filter(transaction -> transaction.getYear() == 2011)
                .sorted(Comparator.comparing(Transaction::getValue))
                .collect(Collectors.toList());
        System.out.println(tr2011);


        // 代码清单-p1ch05b-104页b：交易员都在哪些不同的城市工作过
        logger.info("...交易员都在哪些不同的城市工作过...");
        List<String> cities = transactions.stream()
                .map(transaction -> transaction.getTrader().getCity())
                .distinct()
                .collect(Collectors.toList());
        System.out.println(cities);


        // 代码清单-p1ch05b-105页a：查找所有来自于剑桥的交易员，并按姓名排序
        logger.info("...查找所有来自于剑桥的交易员，并按姓名排序...");
        List<Trader> traders = transactions.stream()
                .map(Transaction::getTrader)
                .filter(trader -> trader.getCity().equals("Cambridge"))
                .distinct()
                .sorted(Comparator.comparing(Trader::getName))
                .collect(Collectors.toList());
        System.out.println(traders);


        // 代码清单-p1ch05b-105页b：返回所有交易员的姓名字符串，按字母顺序排序
        logger.info("...返回所有交易员的姓名字符串，按字母顺序排序...");
        String traderStr = transactions.stream()
                .map(transaction -> transaction.getTrader().getName())
                .distinct()
                .sorted()
                .reduce("", (n1, n2) -> n1 + n2);
        System.out.println(traderStr);


        // 代码清单-p1ch05b-105页c：有没有交易员是在米兰工作的
        logger.info("...有没有交易员是在米兰工作的...");
        boolean milanBased = transactions.stream()
                .anyMatch(transaction -> transaction.getTrader().getCity().equals("Milan"));
        System.out.println(milanBased);


        // 代码清单-p1ch05b-105页d：打印生活在剑桥的交易员的所有交易额
        logger.info("...打印生活在剑桥的交易员的所有交易额...");
        transactions.stream()
                .filter(t -> "Cambridge".equals(t.getTrader().getCity()))
                .map(Transaction::getValue)
                .forEach(System.out::println);


        // 代码清单-p1ch05b-106页a：所有交易中，最高的交易额是多少
        logger.info("...所有交易中，最高的交易额是多少...");
        int highestValue = transactions.stream()
                .map(Transaction::getValue)
                .reduce(0, Integer::max);
        System.out.println(highestValue);


        // 代码清单-p1ch05b-106页b：找到交易额最小的交易
        logger.info("...找到交易额最小的交易...");
        Optional<Transaction> smallestTransaction = transactions.stream()
                .min(Comparator.comparing(Transaction::getValue));
        System.out.println(smallestTransaction.map(String::valueOf).orElse("No transactions found"));

    }

    private static List<Transaction> getTransactions() {

        Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario", "Milan");
        Trader alan = new Trader("Alan", "Cambridge");
        Trader brian = new Trader("Brian", "Cambridge");

        return Arrays.asList(
                new Transaction(brian, 2011, 300),
                new Transaction(raoul, 2012, 1000),
                new Transaction(raoul, 2011, 400),
                new Transaction(mario, 2012, 710),
                new Transaction(mario, 2012, 700),
                new Transaction(alan, 2012, 950)
        );
    }

}
